<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dssp="http://schemas.microsoft.com/xw/2004/10/dssp.html"
    xmlns:s="http://www.w3.org/2003/05/soap-envelope"
    xmlns:contactsensor="http://schemas.vexrobotics.com/2011/09/vexservice.contactsensor.html" >

  <xsl:import href="/resources/dss/Microsoft.Dss.Runtime.Home.MasterPage.xslt" />

  <xsl:template match="/">
    <xsl:comment>
      <!-- Service Header Info -->
    </xsl:comment>
    <xsl:variable name="title">
      VEX Contact Sensors
    </xsl:variable>
    <xsl:variable name="serviceName">
      VEX Contact Sensors
    </xsl:variable>
    <xsl:variable name="description">
      Provides access to the VEX touch or sonar sensors when used as a bumper or contact sensors.
    </xsl:variable>

    <xsl:call-template name="MasterPage">
      <xsl:with-param name="serviceName" select="$serviceName" />
      <xsl:with-param name="description" select="$description" />
      <!-- If title is not provided, serviceName will be used instead. -->
      <xsl:with-param name="title">
        <xsl:value-of select="$serviceName" />
        <xsl:if test="$title != ''">
          <xsl:text> - </xsl:text>
          <xsl:value-of select="$title" />
        </xsl:if>
      </xsl:with-param>
      <!-- Possible values for navigation are: 'Open', 'Closed', and 'None'
           'Open' is the default value. -->
      <xsl:with-param name="navigation" select="'Open'" />
      <!-- The contents of head param will be placed just before the </head> tag in html. -->
      <xsl:with-param name="head">
        <style type="text/css">
          /* Service-specific stylesheet goes here */
        </style>
        <script language="javascript" type="text/javascript" src="/resources/VEXService.Y2011.M09/VEXService.config.Common.js"/>
        <script language="javascript" type="text/javascript">
          <xsl:text>
var pollingFrequency = 1000;
</xsl:text>

          <![CDATA[
function onState(state)
{
    var i;
    var sensors = state.Sensors;
    var cnt = sensors["VEXBumperConfig_Cnt"];
    for(i=1; i<=cnt; ++i)
    {
      var aSensor = sensors["VEXBumperConfig_" + i];
      document.all("pressed_" + i).innerText = aSensor.Pressed;
      if(aSensor.Pressed != "false")
      {
        document.all("pressed_" + i).style.backgroundColor = "#9ecba3";
      }
      else
      {
        document.all("pressed_" + i).style.backgroundColor = "#f67f7f";
      }
      
      document.all("timestampCell_" + i).innerText = aSensor.TimeStamp;
    }
    readState(pollingFrequency);
}

function startPolling()
{
  setStateCompletion("onState");
  readState(pollingFrequency);
}

setTimeout("startPolling()", pollingFrequency);

          ]]>
        </script>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="contactsensor:VEXBumperState">
    <form method="POST" action="" name="SetForm">
      <input type="hidden" name="Action" value="VEXGPIO" />
      <table width="100%" border="0" cellpadding="5" cellspacing="5">
        <tr>
          <th colspan="2">
            <xsl:text disable-output-escaping="yes">VEX Contact Sensors</xsl:text>
          </th>
        </tr>
        <xsl:apply-templates select="contactsensor:Sensors"/>
      </table>
      <input id="Button2" type="reset" value="Reset" name="buttonReset" title="Reset Configuration"/>
      <input type="submit" value="Set Values" name="SetValues" title="Set The Output States"/>
    </form>
  </xsl:template>
  
  <xsl:template match="contactsensor:Sensors/contactsensor:VEXBumperConfig">
    <tr>
      <xsl:attribute name="class">
        <xsl:choose>
          <xsl:when test="position() mod 2 = 0">odd</xsl:when>
        </xsl:choose>
      </xsl:attribute>
      <td>
        <table width="100%" border="0" cellpadding="5" cellspacing="5">
          <tr class="even">
            <th width="20%">
              Name:
            </th>
            <td width="80%">
              <input type="text" name="Name" size="80">
                <xsl:attribute name="name">
                  <xsl:value-of select="'Name_'"/>
                  <xsl:value-of select="position()"/>
                </xsl:attribute>
                <xsl:attribute name="value">
                  <xsl:value-of select="contactsensor:Name"/>
                </xsl:attribute>
              </input>
            </td>
          </tr>
          
          <tr class="odd">
            <th width="20%">
              Sensor Port:
            </th>
            <td width="80%">
              <xsl:value-of select="contactsensor:SensorPort"/>
            </td>
          </tr>
          
          <tr class="even">
            <th width="20%">
              Sensor Type:
            </th>
            <td width="80%">
              <xsl:value-of select="contactsensor:SensorType"/>
            </td>
          </tr>
          
          <tr class="odd">
            <th width="20%">
              Threshold Low:
            </th>
            <td width="80%">
              <xsl:value-of select="contactsensor:ThresholdLow"/>
            </td>
          </tr>
          
          <tr class="even">
            <th width="20%">
              Threshold High:
            </th>
            <td width="80%">
              <xsl:value-of select="contactsensor:ThresholdHigh"/>
            </td>
          </tr>
          
          <tr class="odd">
            <th width="20%">
              Pressed:
            </th>
            <td width="80%">
              <xsl:choose>
                <xsl:when test='contactsensor:Pressed = "true"'>
                  <xsl:attribute name="bgcolor">
                    <xsl:text disable-output-escaping="yes">#9ecba3</xsl:text>
                  </xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:attribute name="bgcolor">
                    <xsl:text disable-output-escaping="yes">#f67f7f</xsl:text>
                  </xsl:attribute>
                </xsl:otherwise>
              </xsl:choose>
              <xsl:attribute name="id">
                <xsl:value-of select="'pressed_'"/>
                <xsl:value-of select="position()"/>
              </xsl:attribute>
              <xsl:value-of select="contactsensor:Pressed"/>
            </td>
          </tr>

          <tr class="even4">
            <th width="20%">
              TimeStamp:
            </th>
            <td width="80%">
              <xsl:attribute name="id">
                <xsl:value-of select="'timestampCell_'"/>
                <xsl:value-of select="position()"/>
              </xsl:attribute>
              <xsl:value-of select="contactsensor:TimeStamp"/>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </xsl:template>

</xsl:stylesheet>
